package com.wwwarehouse.taskcenter.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.StorageBillBean;

import java.util.ArrayList;

/**
 * @author clannad.sk
 */
public class StorageBillAdapter
        extends RecyclerView.Adapter<StorageBillAdapter.ChooseCarriageHolder> {


    private Context mContext;
    private ArrayList<StorageBillBean.StorageBillItemBean> mList;
    private int mHeight;
    private OnItemClickListener mListener;

    public StorageBillAdapter(ArrayList<StorageBillBean.StorageBillItemBean> list, int height, Context context) {
        mContext = context;
        mList = list;
        mHeight = height;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ChooseCarriageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChooseCarriageHolder(View.inflate(mContext, R.layout.task_center_storage_bill_item, null));
    }

    @Override
    public void onBindViewHolder(final ChooseCarriageHolder holder, final int position) {
        final StorageBillBean.StorageBillItemBean dataBean = mList.get(position);
        if(dataBean == null){
            return;
        }
        holder.idBillItemTitleTv.setText(StringUtils.isNullString(dataBean.getBusinessUnitName()) ? "" : dataBean.getBusinessUnitName());
        holder.idBillItemStatusTv.setText(StringUtils.isNullString(dataBean.getStatusName()) ? "" : dataBean.getStatusName());
        String num = StringUtils.isNoneNullString(dataBean.getQty()) ? "" : dataBean.getQty()
                + (StringUtils.isNullString(dataBean.getUnitName()) ? "" : dataBean.getUnitName());
        holder.idBillItemNumTv.setText(num);
        holder.idBillItemOrderTv.setText("订单号：" + (StringUtils.isNullString(dataBean.getStorageUkid()) ? "" : dataBean.getStorageUkid()));
        holder.idBillItemTimeTv.setText("到货时间：" + (StringUtils.isNullString(dataBean.getRegistTime()) ? "" : dataBean.getRegistTime()));
        holder.idBillItemContentRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener != null){
                    mListener.itemClickListener(position, v, dataBean);
                }
            }
        });
    }

    public interface OnItemClickListener {
        void itemClickListener(int position, View view, StorageBillBean.StorageBillItemBean dataBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ChooseCarriageHolder extends RecyclerView.ViewHolder {

        private RelativeLayout idBillItemContentRl;
        private RelativeLayout idBillItemTitleRl;
        private TextView idBillItemTitleTv;
        private TextView idBillItemStatusTv;
        private TextView idBillItemOrderTv;
        private TextView idBillItemTimeTv;
        private TextView idBillItemNumTv;


        ChooseCarriageHolder(View itemView) {
            super(itemView);
            idBillItemContentRl = (RelativeLayout) itemView.findViewById(R.id.idBillItemContentRl);
            idBillItemTitleRl = (RelativeLayout) itemView.findViewById(R.id.idBillItemTitleRl);
            idBillItemTitleTv = (TextView) itemView.findViewById(R.id.idBillItemTitleTv);
            idBillItemStatusTv = (TextView) itemView.findViewById(R.id.idBillItemStatusTv);
            idBillItemOrderTv = (TextView) itemView.findViewById(R.id.idBillItemOrderTv);
            idBillItemTimeTv = (TextView) itemView.findViewById(R.id.idBillItemTimeTv);
            idBillItemNumTv = (TextView) itemView.findViewById(R.id.idBillItemNumTv);
        }
    }
}
